<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/1
 * Time: 13:08
 */
namespace Admin\Model;
use IDCU\Model\RelationModel;

class RoomModel extends RelationModel
{
    protected $_validate = array(
        array('no','require','房间编号必填',1,'regex',3),
//        array('unit_id','require','单元ID缺失',1,'regex',3),
//        array('name','require','必填',1,'regex',3),
        array('no','/^\d{4}$/','房间编码格式不正确',0,'regex',1),
        array('no,unit_id','checkNo1','房间编码已存在',0,'callback',1),
        array('no,id,unit_id','checkNo2','房间编码已存在',0,'callback',2)
    );

    protected $_auto = array(
    );

    protected $_link = array(
        'Community' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Community',
            'mapping_name' => 'community',
            'foreign_key' => 'community_id'
        ),
        'Zone' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Zone',
            'mapping_name' => 'zone',
            'foreign_key' => 'zone_id'
        ),
        'Building' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Building',
            'mapping_name' => 'building',
            'foreign_key' => 'building_id'
        ),
        'Unit' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Unit',
            'mapping_name' => 'unit',
            'foreign_key' => 'unit_id'
        ),
    );

    public function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
        //判断是否有没有上级
        if($data['unit_id']){
            $unit = D('Unit')->find($data['unit_id']);
            $data['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
            $data['community_id'] = $unit['community_id'];
            $data['zone_id'] = $unit['zone_id'];
            $data['building_id'] = $unit['building_id'];
            if($data['no']){
                $data['no'] = $unit['no'].$data['no'];
            }
        }elseif($data['zone_id']){
            $zone = M('Zone')->where(array('id'=>$data['zone_id']))->find();
            $building = M('Building')->where(array('zone_id'=>$data['zone_id']))->find();
            $unit = M('Unit')->where(array('building_id'=>$building['id']))->find();
            if($unit){
                $data['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
                $data['community_id'] = $unit['community_id'];
                $data['zone_id'] = $unit['zone_id'];
                $data['building_id'] = $unit['building_id'];
                if($data['no']){
                    $data['no'] = $unit['no'].$data['no'];
                }
            }elseif ($building){
                $unit['no'] = $building['no'].sprintf('%01d',($building['children_count']+1));
                $unit['name'] = '';
//                $unit['ename'] = '';
                $unit['status'] = 1;
                $unit['remark'] = '';
                $unit['community_id'] = $building['community_id'];
                $unit['zone_id'] = $building['zone_id'];
                $unit['building_id'] = $building['id'];
                $unit['full_parent_name'] = $building['full_parent_name'].$building['name'];
                $unit['children_count'] = 0;


                $unit['id'] = D('Unit')->add($unit);
                D('Building')->where(array('id'=>$building['id']))->setInc('children_count',1);
                $unit = D('Unit')->relation('rooms')->find($unit['id']);

                $data['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
                $data['community_id'] = $unit['community_id'];
                $data['zone_id'] = $unit['zone_id'];
                $data['building_id'] = $unit['building_id'];
                if($data['no']){
                    $data['no'] = $unit['no'].$data['no'];
                }
            }elseif($building==false){
                $building['no'] = $zone['no'].sprintf('%03d',($zone['children_count']+1));
                $building['name'] = '';
//                $building['ename'] = '';
                $building['status'] = 1;
//                $building['remark'] = '';
                $building['community_id'] = $zone['comminity_id'];
                $building['zone_id'] = $zone['id'];
                $building['full_parent_name'] = $zone['full_parent_name'];
//                $building['full_parent_name'] = $zone['full_parent_name'].$zone['name'];
                $building['children_count'] = 0;

                $building['id'] = D('Building')->add($building);
                D('Zone')->where(array('id'=>$zone['id']))->setInc('children_count',1);
                $building = D('Building')->relation('units')->find($building['id']);

                $unit['no'] = $building['no'].sprintf('%01d',($building['children_count']+1));
                $unit['name'] = '';
//                $unit['ename'] = '';
                $unit['status'] = 1;
                $unit['remark'] = '';
                $unit['community_id'] = $building['community_id'];
                $unit['zone_id'] = $building['zone_id'];
                $unit['building_id'] = $building['id'];
                $unit['full_parent_name'] = $building['full_parent_name'].$building['name'];
                $unit['children_count'] = 0;


                $unit['id'] = D('Unit')->add($unit);
                D('Building')->where(array('id'=>$building['id']))->setInc('children_count',1);
                $unit = D('Unit')->relation('rooms')->find($unit['id']);

                $data['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
                $data['community_id'] = $unit['community_id'];
                $data['zone_id'] = $unit['zone_id'];
                $data['building_id'] = $unit['building_id'];
                if($data['no']){
                    $data['no'] = $unit['no'].$data['no'];
                }
            }
        }
    }

    public function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
        M('Unit')->where(array('id'=>$data['unit_id']))->setInc('children_count',1);
    }

    function checkNo1($param){
        $rooms = $this->field('no')->where(array('unit_id' => $param['unit_id']))->select();
        foreach($rooms as $k=>$v){
            if(substr($v['no'],-4) == $param['no']){
                return false;
            }
        }
        return true;
    }

    function checkNo2($param){
        $rooms = $this->field('no')->where(array('unit_id' => $param['unit_id'],'id'=>array('neq',$param['id'])))->select();
        foreach($rooms as $k=>$v){
            if(substr($v['no'],-4) == $param['no']){
                return false;
            }
        }
        return true;
    }
}
